#include "textgenerator.h"

TextGenerator::TextGenerator(QObject *parent) :
    QObject(parent)
{
    clear();
}

TextGenerator::TextGenerator(const QString &file, QObject *parent)
{
    clear();
    setParent(parent);
    loadSourceFromFile(file);
}

QString TextGenerator::getText() const
{
    return _out;
}


void TextGenerator::setSourceSimbols(const QString &source)
{
    QString pre =source;
    pre = pre.replace(QRegExp("[\r\n]+"),"");
    for(int i = 0; i < pre.size(); ++i)
        _simbols.insert(_simbols.size(), pre.mid(i,1));
}

void TextGenerator::loadSourceFromFile(const QString &file)
{
    QFile fl(file);
    if(fl.exists())
    {
        while(!fl.atEnd())
            setSourceSimbols(fl.readLine(4096));
    }
    else qDebug()<<tr("TextGenerator: File %1 not found!").arg(file);
}

void TextGenerator::setSimbolsOnGroup(uint cnt)
{
    if(cnt)
        sCnt = cnt;
}

void TextGenerator::setGroupCount(uint cnt)
{
    if(cnt)
        gCnt = cnt;
}

void TextGenerator::generate()
{
    _out.clear();
    int s_cnt = _simbols.size();
    if(s_cnt)
    {
        for(uint i = 0; i < gCnt; ++i)
        {
            for(uint y = 0; y < sCnt; ++y)
                _out += _simbols.value(qrand()%s_cnt);

            if(i + 1 != gCnt)
                _out += " ";
        }
    }
}

void TextGenerator::clear()
{
    gCnt = 25;
    sCnt = 5;
    _out.clear();
    _simbols.clear();
}
